﻿using CardMonitor.Model;
using CardMonitor.Utils;
using CardMonitor.Windows;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;
using DotNetSpeech;
using System.Windows.Threading;
using System.IO;
using ZXing.QrCode;
using ZXing;
using ZXing.QrCode.Internal;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace CardMonitor.Pages
{
    /// <summary>
    /// Index.xaml 的交互逻辑
    /// </summary>
    public partial class Index : Page
    {
        private NewCardInfo newCardInfo = null;
        private CardInfo objIDCardInfo = null;
        private Page currentPage = null;
        private MainWindow mainWindow = null;
        private SpVoice speech = new SpVoice();
        private string[] CardInfo = new string[30];

        private IntPtr icPtr = IntPtr.Zero;

        public Index(MainWindow _win)
        {
            InitializeComponent();

            currentPage = this;
            string WorkMode = (string)App.getConfig().WorkMode;

            mainWindow = _win;

            this.Loaded += Page_loaded;
        }

        private void showMessage(String message)
        {
            InfoWindow infoWindow = new InfoWindow(message);
            infoWindow.Owner = mainWindow;
            infoWindow.ShowDialog();
        }

        private void Page_loaded(object sender, RoutedEventArgs e)
        {
        }

        private void doCard(object sender, RoutedEventArgs e)//读卡
        {
            string cardDevice = (string)App.getConfig().IDCardDevice;
            if (!"1".Equals(cardDevice))
            {
                goMakeCard((string)App.getConfig().IDCardCode, "", "");
                return;
            }
            int code = NewIDcardread.CardAuthentication();//身份认证
            if (code == 1)
            {
                newCardInfo = NewIDcardread.ReadIDcard();
                if (newCardInfo == null)
                {
                    showMessage("未读取到身份证信息");
                }
                else
                {
                    goMakeCard(newCardInfo.CardNo, newCardInfo.Name, newCardInfo.driverMacCard);
                }
            }
            else
            {
                showMessage("请重新放置身份证");
            }
        }

        /// <summary>
        /// 查询单据按钮
        /// </summary>
        /// <param name="sender"> </param>
        /// <param name="e">      </param>

        private void doCardSettlement(object sender, RoutedEventArgs e)//读卡
        {
            IDCardInfo iDCardInfo = readCard();
            if (iDCardInfo.IDNumber != "" && iDCardInfo.IDNumber != null)
            {
                goMakeCardSettlement(iDCardInfo.IDNumber);
            }
            else
            {
                showMessage("请重新放置身份证");
            }
        }

        private void goMakeCardSettlement(String facId)//查询未结算单据
        {
            HttpPostWindow httpPostWindow = new HttpPostWindow();
            httpPostWindow.Owner = mainWindow;

            JObject paramJSON = new JObject();
            paramJSON.Add("facId", facId);
            httpPostWindow.setParamObject(paramJSON);

            httpPostWindow.setOperation(HttpPostWindow.OperationType.getBillByCardSettlement.ToString());
            bool result = (bool)httpPostWindow.ShowDialog();
            if (!result)
            {
                string message = httpPostWindow.getMessage();
                if (null == message || "".Equals(message))
                {
                    message = "未查询到单据";
                }
                speech.Speak(message, SpeechVoiceSpeakFlags.SVSFlagsAsync);

                showMessage(message);

                return;
            }
            //mainWindow.Speak("请仔细核对计划业务类型及计划信息，确认无误后点击对应业务报到按钮，若无对应业务请返回首页");
            JObject resultArray = httpPostWindow.getResultObject();
            string Message = resultArray["Message"]?.Value<string>();
            ;

            // 创建并显示提示窗口
            var messageWindow = new SettlementInquiry(Message);
            messageWindow.Show(); // 非阻塞式显示
        }

        private void goMakeCard(String cardCode, String name, String driverMacCard)
        {
            HttpPostWindow httpPostWindow = new HttpPostWindow();
            httpPostWindow.Owner = mainWindow;

            JObject paramJSON = new JObject();
            paramJSON.Add("driverCard", cardCode);
            paramJSON.Add("operation", "forMakeCard");
            paramJSON.Add("driverMacCard", driverMacCard);
            httpPostWindow.setParamObject(paramJSON);

            httpPostWindow.setOperation(HttpPostWindow.OperationType.getBillByCard.ToString());
            bool result = (bool)httpPostWindow.ShowDialog();
            if (!result)
            {
                string message = httpPostWindow.getMessage();
                if (null == message || "".Equals(message))
                {
                    message = "未查询到单据";
                }
                speech.Speak(message, SpeechVoiceSpeakFlags.SVSFlagsAsync);

                showMessage(message);

                return;
            }
            mainWindow.Speak("请仔细核对计划业务类型及计划信息，确认无误后点击对应业务报到按钮，若无对应业务请返回首页");
            JArray resultArray = httpPostWindow.getResultArray();
            List<BillModel> billModel = JsonConvert.DeserializeObject<List<BillModel>>(resultArray.ToString());
            SaleCards card = new SaleCards(billModel, mainWindow);
            /*            if (null != image)
                        {
                            card.setPersonImage(image);
                        }*/
            NavigationService.GetNavigationService(this).Navigate(card);
        }

        private void ReplaceCard(object sender, RoutedEventArgs e)
        {
            ReplaceCard replaceCard = new ReplaceCard(mainWindow);
            NavigationService.GetNavigationService(this).Navigate(replaceCard);
        }

        /// <summary>
        /// 打印磅单按钮
        /// </summary>
        /// <param name="sender"> </param>
        /// <param name="e">      </param>
        private void doPrint(object sender, RoutedEventArgs e)
        {
            IDCardInfo iDCardInfo = readCard();
            if (iDCardInfo.IDNumber != "" && iDCardInfo.IDNumber != null)
            {
                goBillPrint(iDCardInfo.IDNumber);
            }
            else
            {
                showMessage("请重新放置身份证");
            }
        }

        /// <summary>
        /// 查询磅单数据
        /// </summary>
        /// <param name="facId"> </param>
        private void goBillPrint(String facId)
        {
            HttpPostWindow httpPostWindow = new HttpPostWindow();
            httpPostWindow.Owner = mainWindow;

            JObject paramJSON = new JObject();
            paramJSON.Add("facId", facId);
            paramJSON.Add("operation", "clientPrint");
            paramJSON.Add("printDuration", App.getConfig().printDuration);
            httpPostWindow.setParamObject(paramJSON);

            httpPostWindow.setOperation(HttpPostWindow.OperationType.getBillsByCardForPrint.ToString());
            bool result = (bool)httpPostWindow.ShowDialog();
            if (!result)
            {
                string message = httpPostWindow.getMessage();
                if (null == message || "".Equals(message))
                {
                    message = "请到人工窗口打印";
                }
                speech.Speak(message, SpeechVoiceSpeakFlags.SVSFlagsAsync);

                showMessage(message);

                return;
            }
            JArray resultArray = httpPostWindow.getResultArray();
            // MessageBox.Show(resultArray.ToString()); Console.WriteLine(resultArray.ToString());doAccount

            List<BillModel> bills = JsonConvert.DeserializeObject<List<BillModel>>(resultArray.ToString());
            if (null == bills || bills.Count == 0)
            {
                showMessage("未查询到单据");
                return;
            }
            PrintPage printPage = new PrintPage(bills, mainWindow);
            NavigationService.GetNavigationService(this).Navigate(printPage);

            //PrintItemPage printItemPage = new PrintItemPage(mainWindow, bills[0]);
            //NavigationService.GetNavigationService(this).Navigate(printItemPage);
        }

        /// <summary>
        /// 读取身份证信息
        /// </summary>
        /// <returns> </returns>
        private IDCardInfo readCard()
        {
            IDCardInfo iDCardInfo = new IDCardInfo();
            if ("1".Equals(App.getConfig().IDCardDevice))//身份证读卡器读卡
            {
                string cardReader = App.getConfig().cardReader;

                if ("1".Equals(cardReader))//精伦读卡器
                {
                    NewIDcardread.CardAuthentication();
                    NewCardInfo newCardInfo = NewIDcardread.ReadIDcard();
                    if (newCardInfo != null)
                    {
                        iDCardInfo.IDNumber = newCardInfo.CardNo;
                        iDCardInfo.MacCard = newCardInfo.driverMacCard;
                    }
                }
                else if ("2".Equals(cardReader)) //熵机
                {
                    iDCardInfo = IDCardReaderUtil_Sjdll.ReadCard();
                }
            }
            else
            {
                iDCardInfo.IDNumber = App.getConfig().IDCardCode;
                iDCardInfo.MacCard = App.getConfig().driverMacCard;
            }

            return iDCardInfo;
        }
    }
}